from rest_framework import serializers

from Group.models import TeamMemberShip


class TeamIndexMemberInfoSerializers(serializers.ModelSerializer):
    name = serializers.SerializerMethodField(read_only=True)
    isAdmin = serializers.BooleanField(source='is_admin', read_only=True)
    isCreator = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = TeamMemberShip
        fields = (
            'id',
            'name',
            'rating',
            'rank',
            'isAdmin',
            'isCreator',
        )

    def get_name(self, instance):
        if instance.name is None or instance.name.strip() == '':
            return instance.person.username
        return instance.name

    def get_isCreator(self, instance):
        return instance.team.team.creator == instance.person
